package com.cn.kabalifeuser.ui.service.activity;

import android.app.ListActivity;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.core.widget.NestedScrollView;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.cn.kabalifeuser.R;
import com.cn.kabalifeuser.base.IBaseActivity;
import com.cn.kabalifeuser.config.LoginHelper;
import com.cn.kabalifeuser.ui.service.adapter.DetailsCommentListAdapter;
import com.cn.kabalifeuser.ui.service.adapter.HoRecruitListAdapter;
import com.cn.kabalifeuser.ui.service.bean.RecruitListBean;
import com.cn.kabalifeuser.ui.service.bean.ShopInfoBean;
import com.cn.kabalifeuser.utils.GlideImageLoader;
import com.cn.kabalifeuser.utils.RxToast;
import com.cn.kabalifeuser.view.LCardView;
import com.cn.kabalifeuser.view.xx.MyRatingBar;
import com.flyco.roundview.RoundTextView;
import com.google.android.material.appbar.AppBarLayout;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.youth.banner.Banner;
import com.zzhoujay.richtext.ImageHolder;
import com.zzhoujay.richtext.RichText;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by 晓林 on 2020/9/11.
 * function: 商家首页
 */
public class BusinessHomeActivity extends IBaseActivity<BusinessHomeView, BusinessHomePresenter> implements BusinessHomeView {


    Bundle bundle;
    int id;
    @BindView(R.id.banner)
    Banner banner;
    @BindView(R.id.fl_layout)
    FrameLayout flLayout;
    @BindView(R.id.appbar)
    AppBarLayout appbar;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_area_info)
    TextView tvAreaInfo;
    @BindView(R.id.tv_juli)
    TextView tvJuli;
    @BindView(R.id.ratingbar)
    MyRatingBar ratingbar;
    @BindView(R.id.ll_btn_recruit_many)
    LinearLayout llBtnRecruitMany;
    @BindView(R.id.rv_recruits)
    RecyclerView rvRecruits;
    @BindView(R.id.ll_recruit_list)
    LCardView llRecruitList;
    @BindView(R.id.tv_comment_num)
    TextView tvCommentNum;
    @BindView(R.id.ll_btn_comment_num)
    LinearLayout llBtnCommentNum;
    @BindView(R.id.rv_comments)
    RecyclerView rvComments;
    @BindView(R.id.ll_comment_list)
    LCardView llCommentList;
    @BindView(R.id.tv_content)
    TextView tvContent;
    @BindView(R.id.vp_type)
    NestedScrollView vpType;
    //    @BindView(R.id.refreshLayout)
//    SmartRefreshLayout refreshLayout;
    @BindView(R.id.tv_btn_case)
    RoundTextView tvBtnCase;
    @BindView(R.id.tv_recruits_num)
    TextView tv_recruits_num;
    @BindView(R.id.tv_order_count)
    RoundTextView tv_order_count;


    List<ShopInfoBean.ResultBean.CommentBean> mList;
    DetailsCommentListAdapter mAdapter;


    List<RecruitListBean.ResultBean.DataBean> mListRecruit;
    HoRecruitListAdapter mAdapterRecruit;

    @Override
    protected BusinessHomePresenter createPresenter() {
        return new BusinessHomePresenter();
    }

    @Override
    protected int provideContentViewId() {
        return R.layout.activity_business_home;
    }

    @Override
    public void initView(Bundle savedInstanceState) {
        super.initView(savedInstanceState);
        bundle = getIntent().getExtras();
        id = bundle.getInt("id");
        appbar.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {
            @Override
            public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
//                float percent = Float.valueOf(Math.abs(verticalOffset)) / Float.valueOf(appBarLayout.getTotalScrollRange());
                //第一种
                int toolbarHeight = appBarLayout.getTotalScrollRange();
                int dy = Math.abs(verticalOffset);
                if (dy <= toolbarHeight) {
                    float scale = (float) dy / toolbarHeight;
                    float alpha = scale * 255;
                    flLayout.setBackgroundColor(Color.argb((int) alpha, 249, 140, 103));
//                    tvTitle.setTextColor(Color.argb((int) alpha, 0, 0, 0));
                }
                //第二种
            }
        });
        mList = new ArrayList<>();
        LinearLayoutManager layoutManager = new LinearLayoutManager(getMe());
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        layoutManager.setSmoothScrollbarEnabled(true);
        layoutManager.setAutoMeasureEnabled(true);
        rvComments.setLayoutManager(layoutManager);
        rvComments.setNestedScrollingEnabled(false);
        //创建适配器
        mAdapter = new DetailsCommentListAdapter(R.layout.comments_item, mList, this);
        //给RecyclerView设置适配器
        rvComments.setAdapter(mAdapter);


        mListRecruit = new ArrayList<>();
        LinearLayoutManager layoutManager1 = new LinearLayoutManager(getMe());
        layoutManager1.setOrientation(LinearLayoutManager.VERTICAL);
        layoutManager1.setSmoothScrollbarEnabled(true);
        layoutManager1.setAutoMeasureEnabled(true);
        rvRecruits.setLayoutManager(layoutManager1);
        rvRecruits.setNestedScrollingEnabled(false);
        //创建适配器
        mAdapterRecruit = new HoRecruitListAdapter(R.layout.recruit, mListRecruit);
        //给RecyclerView设置适配器
        rvRecruits.setAdapter(mAdapterRecruit);


        mPresenter.onShopInfoData(LoginHelper.getLonginData().getResult(), LoginHelper.getLocationInfo().getLontitude(), LoginHelper.getLocationInfo().getLatitude(), id);
        mPresenter.onRecruitListData(LoginHelper.getLonginData().getResult(), 1, 2, id);

    }

    @OnClick({R.id.ll_btn_left, R.id.ll_btn_recruit_many, R.id.ll_btn_comment_num, R.id.tv_btn_case, R.id.tv_btn_order})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ll_btn_left:
                finish();
                break;
            case R.id.ll_btn_recruit_many:
                //todo  招聘更多
                Bundle bundle = new Bundle();
                bundle.putInt("id", id);
                $startActivity(RecruitListActivity.class, bundle);
                break;
            case R.id.ll_btn_comment_num:
                //todo  评论更多
                bundle = new Bundle();
                bundle.putInt("id", id);
                $startActivity(BusinessHomeCommentListActivity.class, bundle);
                break;
            case R.id.tv_btn_case:
                //todo  服务案例
                bundle = new Bundle();
                bundle.putInt("id", id);
                $startActivity(ServiceCaseListActivity.class, bundle);
                break;
            case R.id.tv_btn_order:
                bundle = new Bundle();
                bundle.putInt("id", id);
                $startActivity(ServiceAddActivity.class, bundle);
                break;
        }
    }

    @Override
    public void onReLoggedIn(String msg) {
        RxToast.error(msg);
        LoginHelper.againGoToLoginActivity(getMe());
    }

    @Override
    public void onError(String msg) {
        RxToast.error(msg);
    }

    @Override
    public Context _getContext() {
        return null;
    }

    @Override
    public void onShopInfoSuccess(ShopInfoBean bean) {
        banner.setImageLoader(new GlideImageLoader());
        //设置图片集合
        banner.setImages(bean.getResult().getImg());
        //动画效果
        //banner.setBannerAnimation(Transformer.Accordion);
        //banner设置方法全部调用完毕时最后调用
        banner.start();
        tvTitle.setText(bean.getResult().getTitle());
        tvAreaInfo.setText(bean.getResult().getArea_info());
        tvJuli.setText("距离您" + bean.getResult().getJuli());
        tv_order_count.setText("单量:" + bean.getResult().getOrder_count());
        ratingbar.setEnabled(false);


        ratingbar.setRating(TextUtils.isEmpty(bean.getResult().getFraction()) ? 0 : Float.parseFloat(bean.getResult().getFraction()));

        if (bean.getResult().getComment().size() > 0) {
            llCommentList.setVisibility(View.VISIBLE);
            mAdapter.replaceData(bean.getResult().getComment());
        } else llCommentList.setVisibility(View.GONE);


//        RichText.from(bean.getResult().getInfo()).autoFix(false).showBorder(false).autoPlay(true).into(tvContent);
//        RichText.from(bean.getResult().getInfo()).bind(this).into(tvContent);
        if (!TextUtils.isEmpty(bean.getResult().getInfo())) {
            tvContent.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
            RichText.fromHtml(bean.getResult().getInfo()).autoFix(false).showBorder(false).autoPlay(true).scaleType(ImageHolder.ScaleType.fit_center) // 图片缩放方式
                    .size(ImageHolder.MATCH_PARENT, ImageHolder.WRAP_CONTENT).into(tvContent);
        }

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        RichText.clear(this);
    }

    @Override
    public void onRecruitListSuccess(RecruitListBean bean) {
        if (bean.getResult().getData().size() > 0) {
            llRecruitList.setVisibility(View.VISIBLE);
            tv_recruits_num.setText(bean.getResult().getCount() + "");
            mAdapterRecruit.replaceData(bean.getResult().getData());
        } else {
            llRecruitList.setVisibility(View.GONE);
        }

    }

}
